﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class ClassTypeController : Controller
    {
        //
        // GET: /ClassType/

        public ActionResult ClassTypeList()
        {
            return View();
        }
        //查询
        public ActionResult List(BD_ClassType CT)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field = "DelState", Operator = "=", Value = "0" });
            Expression<Func<BD_ClassType, bool>> exp = SearchExpression.FindByGroup<BD_ClassType>(CT, listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = BLLSession.Current.BD_ClassTypeBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, t => t.ID, true);
            count = BLLSession.Current.BD_ClassTypeBLL.GetCount(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }
        //添加
        public ActionResult AddClassType(BD_ClassType CT)
        {
            var list1 = DALSession.Current.BD_ClassTypeDAL.GetListBy(d => d.ClassType == CT.ClassType && d.DelState == "0");
            if (list1.Count == 0)
            {
                CT.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                CT.EditPerName = OperateContext.sessUserInfo.Name;
                CT.EditPerID = OperateContext.sessUserInfo.Id;
                CT.DelState = "0";
                BLLSession.Current.BD_ClassTypeBLL.Add(CT);
                return Json(new Message() { msg = "保存成功", state = "success" });
            }
            else
            {
                return Json(new Message() { msg = "数据已存在！", state = "warning" });
            }
        }
        //修改
        public ActionResult EditClassType(BD_ClassType CT)
        {
            CT.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            CT.EditPerName = OperateContext.sessUserInfo.Name;
            CT.EditPerID = OperateContext.sessUserInfo.Id;
            BLLSession.Current.BD_ClassTypeBLL.ModifyBy(CT, t => t.ID == CT.ID, "ClassType", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        //删除
        public ActionResult DelClassType(int Id)
        {
            //BLLSession.Current.BD_ClassTypeBLL.DelBy(d => d.ID == Id);
            BD_ClassType BD_ClassType1 = DALSession.Current.BD_ClassTypeDAL.GetListBy(d => d.ID == Id).FirstOrDefault();
            BD_ClassType1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BD_ClassType1.EditPerName = OperateContext.sessUserInfo.Name;
            BD_ClassType1.EditPerID = OperateContext.sessUserInfo.Id;
            BD_ClassType1.DelState = "1";
            BLLSession.Current.BD_ClassTypeBLL.ModifyBy(BD_ClassType1, t => t.ID == BD_ClassType1.ID, "ClassType", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "删除成功", state = "success" });
        }
          //获取开设班级类型
        public void GetAllClassType()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_ClassTypeDAL.GetListBy(t => t.DelState == "0" ).Select(t => new { value = t.ID, text = t.ClassType })));
        }
        //获取特定ID的班级类型
        public void GetAllClassTypeName()
        {
            string classTypeID = Request.Form["classTypeID"];
            int classTypeIDint = Convert.ToInt32(classTypeID);
            var con = DALSession.Current.BD_ClassTypeDAL.GetListBy(c => c.ID == classTypeIDint);
            if (con.Count > 0)
            {
                Response.Write(JsonHelper.toJson(con.FirstOrDefault()));
            }
            else
            {
                Response.Write(JsonHelper.toJson(new { ID = classTypeID, classType = classTypeID }));
            }
        }
    }
}
